package com.msb.juc.c_012;

/**
 * 懒汉式
 * 虽然达到了按需初始化的要求，却带来了线程安全的问题
 */
public class Singleton_Mgr03 {

    private static Singleton_Mgr03 INSTANCE;

    public static Singleton_Mgr03 getInstance() {
        if (INSTANCE == null) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new Singleton_Mgr03();
        }
        return INSTANCE;
    }

    public void m() {
        System.out.println("m");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(Singleton_Mgr03.getInstance().hashCode())).start();
        }
    }
}
